package dev.touchswipe.ScreenLock
{
	import dev.touchswipe.ScreenLock.settingsManager.Settings;
	import dev.touchswipe.shared.Animation.Animation;
	import dev.touchswipe.shared.Animation.AnimationEvent;
	
	import flash.desktop.NativeApplication;
	import flash.display.*;
	import flash.events.Event;
	import flash.events.FullScreenEvent;
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	import flash.geom.Matrix;
	import flash.net.SharedObject;
	import flash.net.URLRequest;
	import flash.net.registerClassAlias;
	import flash.ui.Multitouch;
	import flash.ui.MultitouchInputMode;
	import flash.utils.ByteArray;
	
	import mx.utils.SHA256;
	
	public class UnlockWindow extends NativeWindow
	{
		private var _windowContent:Sprite;
		private var settings:Settings;
		private var sliderContainer:Sprite;
		private var clockContainer:Sprite;
		
		public function UnlockWindow()
		{
			var nativeInitOpts:NativeWindowInitOptions=new NativeWindowInitOptions();
			nativeInitOpts.minimizable=false;
			nativeInitOpts.resizable=false;
			nativeInitOpts.systemChrome=NativeWindowSystemChrome.NONE;
			nativeInitOpts.type=NativeWindowType.LIGHTWEIGHT;
			nativeInitOpts.transparent=true;
			super(nativeInitOpts);
			addEventListener(Event.CLOSING,function(event:Event):void{event.preventDefault();});
			activate();
			maximize();
			
			Multitouch.inputMode=MultitouchInputMode.TOUCH_POINT;
			
			stage.displayState=StageDisplayState.FULL_SCREEN_INTERACTIVE;
			stage.addEventListener(FullScreenEvent.FULL_SCREEN,_onFullScreen);
			stage.align=StageAlign.TOP_LEFT;
			stage.scaleMode=StageScaleMode.NO_SCALE;
			alwaysInFront=true;
			
			loadSettings();
			
			_windowContent=new Sprite();
			stage.addChild(_windowContent);
			_windowContent.graphics.beginFill(0x000000);
			if(settings.background!=""){
				loadBackground(settings.background);
			}
			_windowContent.graphics.drawRect(0,0,stage.stageWidth,stage.stageHeight);
			_windowContent.graphics.endFill();
			
			sliderContainer=new Sprite();
			sliderContainer.graphics.beginFill(0x000000,0.5);
			sliderContainer.graphics.drawRect(0,4*stage.stageHeight/5,stage.stageWidth,stage.stageHeight/5);
			_windowContent.addChild(sliderContainer);
			
			var slider:UnlockSlider=new UnlockSlider();
			sliderContainer.addChild(slider);
			slider.width=stage.stageWidth/2.5;
			slider.x=stage.stageWidth/2-slider.width/2;
			slider.y=9*stage.stageHeight/10-slider.height/2;
			slider.addEventListener(Event.COMPLETE,_hideSlider);
			
			clockContainer=new Sprite();
			if(settings.showClock){
				clockContainer.graphics.beginFill(0x000000,0.5);
				clockContainer.graphics.drawRect(0,0,stage.stageWidth,stage.stageHeight/5);
				_windowContent.addChild(clockContainer);
				
				var clock:Clock=new Clock();
				clockContainer.addChild(clock);
				clock.width=stage.stageWidth;
			}
		}
		
		private var _so:SharedObject;
		public function loadSettings():void{
			registerClassAlias("SettingsManager.Settings",Settings);
			_so=SharedObject.getLocal("settings","/");
			if(_so.data==null||_so.data.version!=Settings.VERSION){
				_so.clear();
				_so.data.version=Settings.VERSION;
				_so.data.settings=new Settings();
				_so.flush();
				settings=Settings(_so.data.settings);
			}
			else{
				settings=Settings(_so.data.settings);
			}
		}
		
		private function loadBackground(path:String):void{
			var fs:FileStream=new FileStream();
			fs.open(new File(path),FileMode.READ);
			var loader:Loader=new Loader();
			var data:ByteArray=new ByteArray();
			fs.readBytes(data);
			loader.loadBytes(data);
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE,function(event:Event):void{
				var bmpData:BitmapData=Bitmap(loader.content).bitmapData;
				var transform:Matrix=new Matrix();
				transform.createBox(stage.stageWidth/bmpData.width,stage.stageHeight/bmpData.height);
				_windowContent.graphics.beginBitmapFill(bmpData,transform,false,true);
				_windowContent.graphics.drawRect(0,0,stage.stageWidth,stage.stageHeight);
				_windowContent.graphics.endFill();
				fs.close();
			});
		}
		private var _pinInput:PinInputPanel;
		private function _hideSlider(event:Event):void{
			var anim:Animation=new Animation(sliderContainer,"y",0,stage.stageHeight/3,5);
			anim.start();
			var anim2:Animation=new Animation(clockContainer,"y",0,-stage.stageHeight/5,5);
			anim2.start();
			anim.addEventListener(AnimationEvent.STOP_ANIMATION,function(event:AnimationEvent):void{
				sliderContainer.visible=false;
				clockContainer.visible=false;
				if(settings.pinHash.length){
					_pinInput=new PinInputPanel();
					_pinInput.x=stage.stageWidth/2-_pinInput.width/2;
					_pinInput.y=stage.stageHeight/2-_pinInput.height/2;
					_pinInput.alpha=0;
					_pinInput.addEventListener(Event.COMPLETE,_validatePin);
					_windowContent.addChild(_pinInput);
					var anim2:Animation=new Animation(_pinInput,"alpha",0,1,10);
					anim2.start();
				}
				else
					_unlock();
			});
			
		}
		
		private function _validatePin(event:Event):void{
			var ba:ByteArray=new ByteArray();
			ba.writeMultiByte(PinInputPanel(event.currentTarget).pin,"us-ascii");
			var hash:String=SHA256.computeDigest(ba);
			if(hash==settings.pinHash)
				_unlock();
			else{
				event.preventDefault();
			}
		}
		
		private function _unlock():void{
			var anim:Animation=new Animation(_windowContent,"y",0,-stage.stageHeight,10);
			anim.start();
			
			anim.addEventListener(AnimationEvent.STOP_ANIMATION,_closeWindow);
		}
		
		private function _closeWindow(event:Event):void{
			close();
		}
		
		private function _onFullScreen(event:FullScreenEvent):void{
			if(!event.fullScreen)
				stage.displayState=StageDisplayState.FULL_SCREEN_INTERACTIVE;
		}
	}
}